package com.xingceng.stock.service.impl;

import com.alibaba.excel.util.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiceng.stock.entity.StockMarketIndexInfo;
import com.xingceng.stock.enums.CodeEnum;
import com.xingceng.stock.exception.StockException;
import com.xingceng.stock.mapper.StockMarketLogPriceMapper;
import com.xingceng.stock.response.ResponseResult;
import com.xingceng.stock.service.StockMarketIndexInfoService;
import com.xingceng.stock.mapper.StockMarketIndexInfoMapper;
import com.xingceng.stock.service.StockMarketLogPriceService;
import com.xingceng.stock.util.DateTimeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZYZ
 * @description 针对表【stock_market_index_info(股票大盘数据详情表)】的数据库操作Service实现
 * @createDate 2025-11-07 14:39:26
 */
@Service
@RequiredArgsConstructor
public class StockMarketIndexInfoServiceImpl extends ServiceImpl<StockMarketIndexInfoMapper, StockMarketIndexInfo>
        implements StockMarketIndexInfoService {

    private final StockMarketIndexInfoMapper stockMarketIndexInfoMapper;

    private final StockMarketLogPriceMapper stockMarketLogPriceMapper;



    @Override
    public ResponseResult<Map> stockTradeVol4InnerMarket() {
        try {
            //1.获取最近的股票交易日时间，精确到分钟 T交易日
            LocalDateTime tDate = DateTimeUtil.getLastDateTime4Stock(LocalDateTime.now());
            //把tDate转换成Date
            Date tDateTime =  Date.from(tDate.atZone(java.time.ZoneId.systemDefault()).toInstant());
            //对应的开盘时间
            LocalTime tOldOpenTime = DateTimeUtil.getOpenDate(tDate).toLocalTime() ;
            Date tOpenTime = Date.from(tOldOpenTime.atDate(tDate.toLocalDate()).atZone(java.time.ZoneId.systemDefault()).toInstant());
            //TODO 后续注释掉 mock-data
            String tDateStr = "20220103143000";
            tDateTime = DateUtils.parseDate(tDateStr, "yyyyMMddHHmmss");
            //mock-data
            String openDateStr = "20220103093000";
            tOpenTime = DateUtils.parseDate(openDateStr, "yyyyMMddHHmmss");
            //获取T-1交易日
            LocalDateTime preTDate = DateTimeUtil.getPreviousTradingDay(tDate);
            Date preTTime = Date.from(preTDate .atZone(java.time.ZoneId.systemDefault()).toInstant());
            Date preTOpenTime = Date.from(preTDate.atZone(java.time.ZoneId.systemDefault()).toInstant());
            //TODO 后续注释掉 mock-data
            String preTStr = "20220102143000";
            preTTime = DateUtils.parseDate(preTStr, "yyyyMMddHHmmss");
            //mock-data
            String openDateStr2 = "20220102093000";
            preTOpenTime = DateUtils.parseDate(openDateStr2, "yyyyMMddHHmmss");
            //股票大盘的编码code集合
            List<String> marketCodes = stockMarketLogPriceMapper.getMarketCodes();
            //2.获取T日的股票大盘交易量统计数据
            List<Map> tData = stockMarketIndexInfoMapper.stockTradeVolCount(marketCodes, tOpenTime, tDateTime);
            //3.获取T-1的数据
            List<Map> preTData = stockMarketIndexInfoMapper.stockTradeVolCount(marketCodes, preTOpenTime, preTTime);
            //4.组装数据
            HashMap<String, List<Map>> data = new HashMap<>();
            data.put("volList", tData);
            data.put("yesVolList", preTData);
            return ResponseResult.ok(data);
        } catch (Exception e) {
            throw new RuntimeException( e);
        }
    }
}




